package com.xiaodeng.edu.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xiaodeng.edu.exception.UserException;
import com.xiaodeng.edu.mapper.AdminMapper;
import com.xiaodeng.edu.pojo.Admin;
import com.xiaodeng.edu.pojo.PageBean;
import com.xiaodeng.edu.pojo.User;
import com.xiaodeng.edu.service.AdminService;
import com.xiaodeng.edu.utils.JwtUtil;
import com.xiaodeng.edu.utils.Md5Util;
import com.xiaodeng.edu.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    //查询管理员
    @Override
    public Admin findByUserName(String adminUser) {
        return adminMapper.findByUserName(adminUser);
    }

    //管理员注册
    @Override
    public void register(String adminUser, String password) {
        //查询管理员
        Admin u = this.findByUserName(adminUser);
        if(u != null){
            throw new UserException(401,"该用户已存在");
        }
        //加密处理
        String md5String = Md5Util.getMD5String(password);
        //注册
        adminMapper.add(adminUser,md5String);
    }

    //管理员登录
    @Override
    public String login(String adminUser, String password) {
        //查询是否存在管理员
        Admin loginuser = this.findByUserName(adminUser);
        System.out.println(loginuser.getAdminUser());
        if(loginuser == null){
            throw new UserException(401,"管理员不存在");
        }
        if (!loginuser.getPassword().equals(Md5Util.getMD5String(password))){
            throw new UserException(402,"账号或者密码错误");
        }
        Map<String,Object> claims = new HashMap<>();
        claims.put("id",loginuser.getId());
        claims.put("admin_user",loginuser.getAdminUser());
        String token = JwtUtil.genToken(claims);
        Map<String, Object> map = JwtUtil.parseToken(token);

        return token;
    }
//分页查询用户
    @Override
    public PageBean<User> userlist(Integer pageNum, Integer pageSize, String username) {
        //创建pageBean对象
        PageBean<User> pb = new PageBean<>();
        //开启分页查询
        PageHelper.startPage(pageNum,pageSize);
        //调用mapper
        List<User> as = adminMapper.userlist(username);
        Page<User> p =(Page<User>) as;
        //把数据填充到pageBean
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    //删除管理员
    @Override
    public void delete(Integer id) {
        adminMapper.delete(id);
    }

    //更新管理员头像
    @Override
    public void updateAvatar(String avatarUrl) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        adminMapper.updateAvatar(avatarUrl,id);
    }

    //更新管理员信息
    @Override
    public void update(Admin admin) {
        adminMapper.update(admin);
    }

    //管理员修改密码
    @Override
    public void updatePwd(String newPwd, Admin admin) {

        //根据用户名拿到原密码跟新密码对比
        Map<String,Object> map = ThreadLocalUtil.get();
        String adminUser = (String) map.get("admin_user");
        Integer id =(Integer) map.get("id");
        System.out.println(adminUser);
        Admin loginUser = this.findByUserName(adminUser);
        if (StrUtil.isBlank(admin.getPassword()) ){
            throw new UserException(401,"原密码为空");
        }
        if (StrUtil.isBlank(loginUser.getPassword())){
            throw new UserException(401,"新密码为空");
        }
        if (!loginUser.getPassword().equals(Md5Util.getMD5String(admin.getPassword()))){
            throw new UserException(401,"请输入正确的原密码");
        }
        adminMapper.updatePwd(Md5Util.getMD5String(newPwd), id);
    }


}
